package com.hhkj.admin.course.service.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.course.domain.Course;
import com.hhkj.admin.course.domain.CoursePriceConf;
import com.hhkj.admin.course.service.base.CoursePriceConfService;
import com.hhkj.admin.course.service.base.CourseService;
import com.hhkj.admin.course.service.biz.SaleCourseBizService;
import com.ruoyi.common.cont.SysConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SaleCourseBizServiceImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2024/1/17 9:25
 */
@Service
public class SaleCourseBizServiceImpl
        implements SaleCourseBizService {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CoursePriceConfService coursePriceConfService;


    @Override
    public List<Course> listFree() {
        List<Course> courseList = new ArrayList<>();
        // 获取免费的课程id信息列表
        LambdaQueryWrapper<CoursePriceConf> priceQueryWrapper = new LambdaQueryWrapper<>();
        priceQueryWrapper.le(CoursePriceConf::getPrice, BigDecimal.ZERO);
        List<CoursePriceConf> priceConfList = coursePriceConfService.list(priceQueryWrapper);
        if (CollectionUtil.isEmpty(priceConfList)) {
            return courseList;
        }
        // 获取对应课程信息
        LambdaQueryWrapper<Course> courseQuery = new LambdaQueryWrapper<>();
        List<Long> parentIdList = priceConfList.stream()
                .map(CoursePriceConf::getCourseId)
                .collect(Collectors.toList());
        courseQuery.in(Course::getParentId, parentIdList)
                .eq(Course::getDisableFlag, SysConst.Flag.NO.getFlag());
        return courseService.list(courseQuery);
    }
}
